package com.fashion.server.service.impl;

import com.fashion.server.common.IConstants;
import com.fashion.server.dao.MapperFactory;
import com.fashion.server.domain.model.WebUserIdentityModel;
import com.fashion.server.domain.request.HttpDataParam;
import com.fashion.server.domain.response.ResponseData;
import com.fashion.server.service.WebUserIdentityService;
import com.fashion.server.utils.JsonUtil;
import com.fashion.server.utils.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by zhuqi on 2016/9/12.
 */
@Service("webUserIdentityService")
public class WebUserIdentityServiceImpl implements WebUserIdentityService {

    private static final Logger logger = Logger.getLogger(WebUserIdentityServiceImpl.class);

    @Resource
    private MapperFactory mapperFactory;


    @Override
    public String updateWebUserIdentity(HttpDataParam<WebUserIdentityModel> dataCondition) {
        // TODO Auto-generated method stub
        List<WebUserIdentityModel> list = dataCondition.getObjects();
        ResponseData responseData = new ResponseData();
        Date date = new Date();
        if(null != list){
            for (WebUserIdentityModel model : list) {
                ResponseData checkData = checkUpdateData(model);
                checkData.setData(model);
                if (IConstants.ResponseDefine.Code.SUCCESS_200.getIndex() != checkData.getCode()) {
                    return JsonUtil.toJSONString(checkData, false);
                }
            }
            for (WebUserIdentityModel model : list) {
                mapperFactory.getWriteWebUserIdentityMapper().updateByUserIdSelective(model);
            }
            responseData.setData(null);
        }else{
            responseData.setCode(IConstants.ResponseDefine.Code.ERROR_402.getIndex());
            responseData.setMsg("参数为空");
        }

        return JsonUtil.toJSONString(responseData, false);
    }

    private ResponseData checkUpdateData(WebUserIdentityModel object) {
        ResponseData responseData = new ResponseData();
        responseData.setCode(IConstants.ResponseDefine.Code.ERROR_402.getIndex());
        responseData.setData(object);
        if (null == object.getId() && StringUtil.isBlank(object.getUserId())) {
            responseData.setMsg("参数错误:Id,userId必须有一个值");
            return responseData;
        }
        return new ResponseData(IConstants.ResponseDefine.Code.SUCCESS_200.getIndex());
    }


}
